//
//  IJSDevice.m
//  IJSFramework
//
//  Created by shange on 2017/4/16.
//  Copyright © 2017年 jinshan. All rights reserved.
//

#import "IJSDevice.h"
//#import "sys/utsname.h"
//获得设备型号
#include <sys/types.h>
#include <sys/sysctl.h>
//获取手机的mac地址
//#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>


@implementation IJSDevice

#pragma mark 获得设备型号
+ (NSString *)getCurrentDeviceModel
{
    int mib[2];
    size_t len;
    char *machine;

    mib[0] = CTL_HW;
    mib[1] = HW_MACHINE;
    sysctl(mib, 2, NULL, &len, NULL, 0);
    machine = malloc(len);
    sysctl(mib, 2, machine, &len, NULL, 0);

    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
    // iPhone
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone2G/iPhone1,1 ";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G/iPhone1,2";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS/iPhone2,1";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone4/iPhone3,1";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone4/iPhone3,2";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone4/iPhone3,3";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S/iPhone4,1";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone5/iPhone5,1";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone5/iPhone5,2";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone5c/iPhone5,3";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone5c/iPhone5,4";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone5s/iPhone6,1";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone5s/iPhone6,2";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone6/iPhone7,2";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus/iPhone7,1";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone6s/iPhone8,1";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone6sPlus/iPhone8,2";
    if ([platform isEqualToString:@"iPhone8,3"]) return @"iPhoneSE/iPhone8,3";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE/iPhone8,4";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone7/iPhone9,1";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus/iPhone9,2";

    //iPod Touch
    if ([platform isEqualToString:@"iPod1,1"]) return @"iPodTouch/iPod1,1";
    if ([platform isEqualToString:@"iPod2,1"]) return @"iPodTouch2G/iPod2,1";
    if ([platform isEqualToString:@"iPod3,1"]) return @"iPodTouch3G/iPod3,1";
    if ([platform isEqualToString:@"iPod4,1"]) return @"iPodTouch4G/iPod4,1";
    if ([platform isEqualToString:@"iPod5,1"]) return @"iPodTouch5G/iPod5,1";
    if ([platform isEqualToString:@"iPod7,1"]) return @"iPodTouch6G/iPod7,1";

    //iPad
    if ([platform isEqualToString:@"iPad1,1"]) return @"iPad/iPad1,1";
    if ([platform isEqualToString:@"iPad2,1"]) return @"iPad2/iPad2,1";
    if ([platform isEqualToString:@"iPad2,2"]) return @"iPad2/iPad2,2";
    if ([platform isEqualToString:@"iPad2,3"]) return @"iPad2/iPad2,3";
    if ([platform isEqualToString:@"iPad2,4"]) return @"iPad2/iPad2,4";
    if ([platform isEqualToString:@"iPad3,1"]) return @"iPad3/iPad3,1";
    if ([platform isEqualToString:@"iPad3,2"]) return @"iPad3/iPad3,2";
    if ([platform isEqualToString:@"iPad3,3"]) return @"iPad3/iPad3,3";
    if ([platform isEqualToString:@"iPad3,4"]) return @"iPad4/iPad3,4";
    if ([platform isEqualToString:@"iPad3,5"]) return @"iPad4/iPad3,5";
    if ([platform isEqualToString:@"iPad3,6"]) return @"iPad4/iPad3,6";

    //iPad Air
    if ([platform isEqualToString:@"iPad4,1"]) return @"iPadAir/iPad4,1";
    if ([platform isEqualToString:@"iPad4,2"]) return @"iPadAir/iPad4,2";
    if ([platform isEqualToString:@"iPad4,3"]) return @"iPadAir/iPad4,3";
    if ([platform isEqualToString:@"iPad5,3"]) return @"iPadAir2/iPad5,3";
    if ([platform isEqualToString:@"iPad5,4"]) return @"iPadAir2/iPad5,4";

    //iPad mini
    if ([platform isEqualToString:@"iPad2,5"]) return @"iPadmini1G/iPad2,5";
    if ([platform isEqualToString:@"iPad2,6"]) return @"iPadmini1G/iPad2,6";
    if ([platform isEqualToString:@"iPad2,7"]) return @"iPadmini1G/iPad2,7";
    if ([platform isEqualToString:@"iPad4,4"]) return @"iPadmini2/iPad4,4";
    if ([platform isEqualToString:@"iPad4,5"]) return @"iPadmini2/iPad4,5";
    if ([platform isEqualToString:@"iPad4,6"]) return @"iPadmini2/iPad4,6";
    if ([platform isEqualToString:@"iPad4,7"]) return @"iPadmini3/iPad4,7";
    if ([platform isEqualToString:@"iPad4,8"]) return @"iPadmini3/iPad4,8";
    if ([platform isEqualToString:@"iPad4,9"]) return @"iPadmini3/iPad4,9";
    if ([platform isEqualToString:@"iPad5,1"]) return @"iPadmini4/iPad5,1";
    if ([platform isEqualToString:@"iPad5,2"]) return @"iPadmini4/iPad5,2";

    if ([platform isEqualToString:@"i386"]) return @"iPhoneSimulator/i386";
    if ([platform isEqualToString:@"x86_64"]) return @"iPhoneSimulator/x86_64";
    // 不满足上面的条件就会直接返回
    return platform;
}
#pragma mark 获取手机mac地址的方法1
- (NSString *)getMacAddressFuncOne
{
    int mib[6];
    size_t len;
    char *buf;
    unsigned char *ptr;
    struct if_msghdr *ifm;
    struct sockaddr_dl *sdl;
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }

    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }

    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3), *(ptr + 4), *(ptr + 5)];
    //  NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    NSLog(@"outString:%@", outstring);
    free(buf);
    return [outstring uppercaseString];
}
#pragma mark 获取手机mac地址的方法二
- (NSString *)getMacAddressFuncTwo
{
    int mgmtInfoBase[6];
    char *msgBuffer = NULL;
    size_t length;
    unsigned char macAddress[6];
    struct if_msghdr *interfaceMsgStruct;
    struct sockaddr_dl *socketStruct;
    NSString *errorFlag = NULL;
    // Setup the management Information Base (mib)
    mgmtInfoBase[0] = CTL_NET;  // Request network subsystem
    mgmtInfoBase[1] = AF_ROUTE; // Routing table info
    mgmtInfoBase[2] = 0;
    mgmtInfoBase[3] = AF_LINK;       // Request link layer information
    mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces

    // With all configured interfaces requested, get handle index
    if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
        errorFlag = @"if_nametoindex failure";
    else {
        // Get the size of the data available (store in len)
        if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
            errorFlag = @"sysctl mgmtInfoBase failure";
        else {
            // Alloc memory based on above call
            if ((msgBuffer = malloc(length)) == NULL)
                errorFlag = @"buffer allocation failure";
            else {
                // Get system information, store in buffer
                if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
                    errorFlag = @"sysctl msgBuffer failure";
            }
        }
    }
    // Befor going any further...
    if (errorFlag != NULL) {
        NSLog(@"Error: %@", errorFlag);
        return errorFlag;
    }
    // Map msgbuffer to interface message structure
    interfaceMsgStruct = (struct if_msghdr *)msgBuffer;
    // Map to link-level socket structure
    socketStruct = (struct sockaddr_dl *)(interfaceMsgStruct + 1);
    // Copy link layer address data in socket structure to an array
    memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);

    // Read from char array into a string object, into traditional Mac address format
    NSString *macAddressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
                                                            macAddress[0], macAddress[1], macAddress[2],
                                                            macAddress[3], macAddress[4], macAddress[5]];
    NSLog(@"Mac Address: %@", macAddressString);
    // Release the buffer memory
    free(msgBuffer);
    return macAddressString;
}


@end
